/*
 *
 * Copyright (c) 2025 Bocloud Technologies Co., Ltd.
 * installer is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain n copy of Mulan PSL v2 at:
 *          http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 *
 */

package initialize

import (
	"encoding/json"
	"os"
	"testing"

	"github.com/stretchr/testify/assert"

	"gopkg.openfuyao.cn/bkecommon/cluster/api/v1beta1"
)

func TestSetDefaultBKEConfig(t *testing.T) {
	cfg := &BkeConfig{
		Nodes: []v1beta1.Node{
			{
				Hostname: "123",
			},
		},
	}
	SetDefaultBKEConfig(cfg)

	assert.Equal(t, cfg.Cluster.CertificatesDir, DefaultCertificatesDir)
	assert.Equal(t, cfg.Cluster.KubernetesVersion, DefaultKubernetesVersion)
	assert.Equal(t, cfg.Cluster.HTTPRepo.Domain, DefaultYumRepo)
	assert.Equal(t, cfg.Cluster.HTTPRepo.Port, DefaultYumRepoPort)
	assert.Equal(t, cfg.Cluster.ImageRepo.Domain, DefaultImageRepo)
	assert.Equal(t, cfg.Cluster.ImageRepo.Port, DefaultImageRepoPort)
	assert.Equal(t, cfg.Cluster.ImageRepo.Prefix, ImageRegistryKubernetes)
	assert.Equal(t, cfg.Cluster.Networking.DNSDomain, DefaultServiceDNSDomain)
	assert.Equal(t, cfg.Cluster.Networking.ServiceSubnet, DefaultServicesSubnet)
	assert.Equal(t, cfg.Cluster.Networking.PodSubnet, DefaultPodSubnet)
	assert.Equal(t, cfg.Cluster.Etcd.DataDir, DefaultEtcdDataDir)
	assert.Equal(t, cfg.Cluster.APIServer.Port, int32(DefaultAPIBindPort))
	assert.EqualValues(t, cfg.Cluster.APIServer.ExtraArgs, map[string]string{"authorization-mode": "Node,RBAC"})
	assert.Equal(t, cfg.Cluster.Kubelet.ManifestsDir, DefaultManifestsDir)
	assert.Equal(t, cfg.Cluster.ContainerRuntime.CRI, CRIContainerd)
	assert.Equal(t, cfg.Cluster.ContainerRuntime.Runtime, DefaultRuntime)
	assert.Equal(t, cfg.Nodes[0].Port, DefaultNodeSSHPort)
	assert.Equal(t, cfg.Nodes[0].Username, DefaultNodeUserRoot)
}

func TestExportDefaults(t *testing.T) {
	cfg := &BkeConfig{}
	SetDefaultBKEConfig(cfg)

	marshal, err := json.Marshal(cfg)
	if err != nil {
		return
	}
	err = os.WriteFile("bkeconfig-default.json", marshal, 0644)
	if err != nil {
		t.Error(err)
	}
}
